<template>
  <div class="box">
    <div class="avater_box">
      <img class="avater" src="../../assets/imgs/avater.png" alt="" />
    </div>
    <div class="login_div">
      <el-form
        :model="user"
        :rules="login_rule"
        ref="ruleForm"
        label-width="100px"
        class="login_form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="user.username"
            placeholder="请输入登录用户"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="user.password"
            placeholder="请输入登录密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "../../api/user";
import { mapMutations } from "vuex";
import { USERLOGIN, USERLOGOUT } from "../../store/types";
export default {
  data() {
    return {
      user: {
        username: "xiaoguo",
        password: "123456",
      },
      login_rule: {
        username: [
          { required: true, message: "请输入登录用户", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    ...mapMutations([USERLOGIN, USERLOGOUT]),
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let result = await login(this.user.username, this.user.password);
          const token = result.data.token;
          if (token != "") {
            //保存token用于权限验证
            this[USERLOGIN](token);
            localStorage.setItem("username", this.user.username);
            this.$router.push("/home");
          } else {
            this.$message({
              message: "你的账号密码错误",
              type: "error",
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.box {
  width: 100%;
  height: 100%;
  background-color: #2d3a4b;
  display: flex;
  justify-content: center;
  align-items: center;

  .login_div {
    width: 550px;
    height: 220px;
    background-color: #fff;
    padding: 100px 20px 20px 0px;
  }

  .avater_box {
    position: relative;
    width: 130px;
    height: 130px;
    background-color: pink;
    left: 340px;
    bottom: 160px;
    z-index: 1;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);

    .avater {
      width: 110px;
      height: 110px;
      border-radius: 50%;
    }
  }
}
</style>